package com.bejoy.coolatin;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.bejoy.coolatin.util.ContextUtil;
import com.bejoy.coolatin.util.StaticProperty;
import com.bejoy.coolatin.volley.InterfaceViaVolleys;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

/**
 * 登录页面
 * @author Tien。
 *
 */
public class LoginActivity extends Activity implements OnClickListener{
	SharedPreferences share = null;
	SharedPreferences.Editor sedit = null;
	ContextUtil contextUtil;
	
	private EditText et_username,et_pwd;
	private Button button_login;
	private Map<String, String> loginParmasMap = new HashMap<String, String>();
	private String account,password;//用户名   密码
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);
		
		share = getSharedPreferences(StaticProperty.SAVEINFO, Activity.MODE_PRIVATE);
		sedit = share.edit();
		contextUtil = new ContextUtil(LoginActivity.this);
		
		// 保存手机信息
		DisplayMetrics displayMetrics = new DisplayMetrics();
		this.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
		int width = displayMetrics.widthPixels;
		int height = displayMetrics.heightPixels;
		sedit.putInt(StaticProperty.SCREENWIDTH, width);// 保存屏幕宽度
		sedit.putInt(StaticProperty.SCREENHEIGHT, height);// 保存屏幕高度
		sedit.putString(StaticProperty.BRAND, android.os.Build.BRAND);// 保存手机品牌
		String deviceId = contextUtil.getDeviceId();
		sedit.putString(StaticProperty.DEVICEID, deviceId);// 保存屏幕宽度
		sedit.commit();
		
		et_username = (EditText) findViewById(R.id.et_login_username);
		et_pwd = (EditText) findViewById(R.id.et_login_pwd);
		button_login = (Button) findViewById(R.id.btn_login);
		
		button_login.setOnClickListener(this);
	}
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_login://点击登陆
			account = et_username.getText().toString().trim();
			password = et_pwd.getText().toString().trim();
			
			loginParmasMap.put("mobile", account);
			loginParmasMap.put("password", password);
			loginParmasMap.put("type", "1");
			loginParmasMap.put("uuid", share.getString(StaticProperty.DEVICEID, ""));
			loginParmasMap.put("device", "Android");
			
			loginServer();
			break;
		}
	}
	//登陆数据服务器
	private void loginServer(){
		new InterfaceViaVolleys(loginParmasMap, StaticProperty.IPADDRESS + "user/login", null,
				new Response.Listener<String>() {

					@Override
					public void onResponse(String response) {
						try {
							JSONObject jsonObject = new JSONObject(response);
							if (jsonObject.getInt("status") == 1 && jsonObject.getString("result") != null 
									&& !"".equals(jsonObject.getString("result"))) {
								JSONObject result = jsonObject.getJSONObject("result");
								String tokenId = result.getString("tokenId");
								String nickName = result.getString("nickName");
								String userHeadImage = result.getString("userPhoto");
								
								sedit.putString(StaticProperty.TOKENID, tokenId);
								sedit.putString(StaticProperty.NICKNAME,
										nickName);
								sedit.putString(StaticProperty.ACCOUNT,
										LoginActivity.this.account);
								sedit.putString(StaticProperty.PASSWORD,
										LoginActivity.this.password);
								sedit.putString(StaticProperty.USERPHOTO,
										userHeadImage);
								sedit.commit();
								Toast.makeText(LoginActivity.this, "恭喜您，登陆成功..", Toast.LENGTH_SHORT).show();
								
								Intent intent = new Intent(LoginActivity.this, PersonalCenterActivity.class);
								startActivity(intent);
							}else {
								Toast.makeText(LoginActivity.this,
										jsonObject.getString("msg"),
										Toast.LENGTH_SHORT).show();
							}
						} catch (JSONException e) {
							e.printStackTrace();
						}
					}
				}, new Response.ErrorListener() {

					@Override
					public void onErrorResponse(VolleyError error) {
						
					}
				}).stringRequest();
	}
}
